package com.itbaizhan.seckill.service.impl;

import com.itbaizhan.common.vo.LivegoodsResult;
import com.itbaizhan.seckill.mapper.SeckillMapper;
import com.itbaizhan.seckill.service.SeckillService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@DubboService
public class SeckillServiceImpl implements SeckillService {
    @Autowired
    private SeckillMapper seckillMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;
//    @Autowired
//    private AmqpTemplate amqpTemplate;


    @Override
    public LivegoodsResult buyaction(Long houseId, String phone) {
        Boolean resule = (Boolean) rabbitTemplate.convertSendAndReceive("livegoods.exchange.seckill", "livegoods.queue.seckill", houseId);
        if (resule!=null&&resule){
            Map<String , Object> params = new HashMap<>();
            params.put("id",houseId);
            params.put("phone",phone);
            rabbitTemplate.convertAndSend("livegoods.exchange.order","",params);
            return LivegoodsResult.ok("预定成功");
        }
        return LivegoodsResult.error();
    }



    @Override
    public LivegoodsResult buytime(Long id) {
        Date date = seckillMapper.selectBuytimeById(id);
        return LivegoodsResult.ok(date.getTime());
    }
}
